class_name CampComponent
extends BaseComponent

# TODO 完善逻辑
var cur_camp:GameEnum.Camp = GameEnum.Camp.None
var target_camps:Array[GameEnum.Camp]:
	get:
		return get_target_camps()
var camp_camps:Array[GameEnum.Camp]:
	get:
		return get_camp_camps()
var master = null
var servant_dict:Dictionary = {} # 记录仆人的Entity和主仆指示线的对象引用
signal on_camp_changed(pre_camp,cur_camp)

func get_target_camps() -> Array[GameEnum.Camp]:
	match cur_camp:
		GameEnum.Camp.Friendly:
			return [GameEnum.Camp.Hostile,GameEnum.Camp.Neutral]
		GameEnum.Camp.Hostile:
			return [GameEnum.Camp.Friendly,GameEnum.Camp.Neutral]
		GameEnum.Camp.Neutral:
			return [GameEnum.Camp.Friendly,GameEnum.Camp.Hostile,GameEnum.Camp.Neutral]
		_:
			return [GameEnum.Camp.Friendly,GameEnum.Camp.Hostile,GameEnum.Camp.Neutral]
func get_camp_camps() -> Array[GameEnum.Camp]:
	if cur_camp == GameEnum.Camp.Neutral:
		return []
	return [cur_camp]
func change_camp(target_camp:GameEnum.Camp):
	if target_camp != cur_camp:
		var pre_camp = cur_camp
		GameCampSystem.group_remove(cur_camp,cur_entity)
		cur_camp = target_camp
		GameCampSystem.group_add(cur_camp,cur_entity)
		var logical_body_area:Area2D = cur_entity.logic_body_area
		match cur_camp:
			GameEnum.Camp.Friendly:logical_body_area.collision_mask = StaticValue.PHYSICS_LAYER_MASK_FRIENDLY
			GameEnum.Camp.Hostile:logical_body_area.collision_mask = StaticValue.PHYSICS_LAYER_MASK_HOSTILE
			GameEnum.Camp.Neutral:logical_body_area.collision_mask = StaticValue.PHYSICS_LAYER_MASK_NEUTRAL
		on_camp_changed.emit(pre_camp,cur_camp)
func add_servant(target):
	pass
func remove_servant(target):
	pass
func remove_all_servant():
	pass
func init_servants():
	pass
func create_MS_line():
	pass

func init_component(entity):
	super.init_component(entity)
	cur_camp = cur_entity.get_entity_data().default_camp
	on_camp_changed.connect(func(pre_camp,cur_camp):
		var agent:NavigationAgent2D = cur_entity.navigation_agent_2d
		match cur_camp:
			GameEnum.Camp.Friendly:
				agent.avoidance_layers = StaticValue.PHYSICS_LAYER_MASK_FRIENDLY
				agent.avoidance_mask = StaticValue.PHYSICS_LAYER_MASK_FRIENDLY
			GameEnum.Camp.Hostile:
				agent.avoidance_layers = StaticValue.PHYSICS_LAYER_MASK_HOSTILE
				agent.avoidance_mask = StaticValue.PHYSICS_LAYER_MASK_HOSTILE
			GameEnum.Camp.Neutral:
				agent.avoidance_layers = StaticValue.PHYSICS_LAYER_MASK_NEUTRAL
				agent.avoidance_mask = StaticValue.PHYSICS_LAYER_MASK_NEUTRAL
		)
	change_camp(cur_camp)
	
func _init() -> void:
	component_name = "CampComponent"
